package com.read.controller;

import com.read.common.annotation.UserLoginToken;
import com.read.common.response.RespEnum;
import com.read.common.response.RespResult;
import com.read.dto.params.RegisterParamsDto;
import com.read.dto.result.JwtUserDto;
import com.read.service.UserService;
import com.read.util.JwtUtil;
import com.read.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author sh-sunb6
 * @description 用户相关 Controller
 * @date 2019-02-21 10:00
 */
@Api(tags = "用户相关 Controller")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * @description 用户注册
     * @author sh-sunb6
     * @date 2019-02-21 10:02
     */
    @ApiOperation(value = "用户注册", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName", value="用户名", required=true, paramType="query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name="password", value="密码", required=true, paramType="query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name="sex", value="性别(1男，2女，0未知)", required=true, paramType="query", dataType = "int", defaultValue = "")
    })
    @PostMapping("/register")
    public RespResult registerUser(RegisterParamsDto paramsDto) {

        userService.registerUser(paramsDto);
        return RespUtil.success();
    }

    /**
     * @description 查询用户名是否可用
     * @author sh-sunb6
     * @date 2019-02-21 13:52
     */
    @ApiOperation(value = "查询用户名是否可用", notes = "true可用；false不可用，已存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName", value="用户名", required=true, paramType="query", dataType = "String", defaultValue = "")
    })
    @GetMapping("/userNameUsable")
    public RespResult userNameIsUsable(String userName) {
        boolean isUsable = userService.isUserNameUsable(userName);
        return RespUtil.success(isUsable);
    }
    
    /** 
     * @description 用户登陆
     * @author sh-sunb6 
     * @date 2019-02-21 15:33
     */
    @ApiOperation(value = "用户登录", notes = "在底部加上联系邮箱：loveread@foxmail.com")
    @ApiImplicitParams({
            @ApiImplicitParam(name="account", value="用户名、手机号、邮箱", required=true, paramType="query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name="password", value="密码", required=true, paramType="query", dataType = "String", defaultValue = "")
    })
    @GetMapping("/login")
    public RespResult loginUser(String account, String password) {
        JwtUserDto jUser = userService.loginUser(account, password);

        //用户名或密码错误
        if (null == jUser) {
            return RespUtil.error(RespEnum.ACCOUNT_PASSWORD_ERROR);
        }

        //账号被锁定 用户状态：1正常；2锁定
        if (jUser.getUserState() == 2) {
            return RespUtil.error(RespEnum.ACCOUNT_LOCK_ERROR);
        }

        return RespUtil.success(JwtUtil.createToken(jUser));
    }

    /**
     * @description 根据token获取用户信息
     * @author sh-sunb6
     * @date 2019-03-05 11:02
     */
    @ApiOperation(value = "根据token获取用户信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "系统token", required = true, paramType = "header")
    })
    @UserLoginToken
    @GetMapping("/userInfo")
    public RespResult getLoginUserInfo(HttpServletRequest request) {
        JwtUserDto jUser = getUserInfo(request);
        return RespUtil.success(jUser);
    }

}
